<?php

namespace Home\Controller;

use Home\Controller\BaseController;

class UsersController extends BaseController {

    public function _initialize() {
        parent::_initialize();

        if (empty($this->user_id)) {
            $this->ajaxReturn(0, "请登录!", '', -1);
        }
    }

    //获取用户信息
    public function userInfo() {
        $userInfo = M()->field("*")
                        ->table("ot_users as u ")
                        ->where(array('user_id' => $this->user_id))->find();

        if ($userInfo) {

            $userInfo['bri'] = '';
            if (!empty($userInfo['brithday'])) {
                $bri = strtotime($userInfo['brithday']);

                $userInfo['bri'] = date('Y-m-d', $bri);
            }
        }
        $this->success('获取成功', $userInfo);
    }

    public function setUserInfo() {
        $real_name = I('real_name');
        $sex = I('sex', -1, 'intval');
        $company = I('company');
        $company_address = I('company_address');
        $position = I('position');
        $company_introduction = I('company_introduction');
        $wx_number = I('wx_number');
        $birthday = I('birthday');
//        $constellation = I('constellation');

        $data = [];
        if ($sex != -1) {
            $data['sex'] = $sex;
        }

        if (!empty($real_name)) {
            $data['real_name'] = $real_name;
        }
        if (!empty($company)) {
            $data['company'] = $company;
        }
        if (!empty($company_address)) {
            $data['company_address'] = $company_address;
        }
        if (!empty($position)) {
            $data['position'] = $position;
        }
        if (!empty($company_introduction)) {
            $data['company_introduction'] = $company_introduction;
        }
        if (!empty($wx_number)) {
            $data['wx_number'] = $wx_number;
        }

        if (!empty($birthday)) {

            if (strtotime($birthday)) {
                $data['brithday'] = $birthday;
            }
             $data['constellation'] = birthext($birthday);
        }
//        if (!empty($constellation)) {
//            $data['constellation'] = $constellation;
//        }

        $res = M('Users')->where(['user_id' => $this->user_id])->save($data);

        $this->success('更新成功', $res);
    }

    public function getMyCarInfo() {
        $car_info = M()->field("*")
                        ->table("ot_users as u ")
                        ->where(array('user_id' => $this->user_id))->find();
        $car = M('Car')
                ->where(['user_id' => $this->user_id])
                ->select();


        if ($car_info) {
            $car_info['car'] = [];
            if ($car) {
                $car_info['car'] = $car;
                $car_info['car_num'] = count($car);
                $car_info['car_str'] = $car[0]['car_str'];
                $car_info['car_number'] = $car[0]['car_number'];
            }

            $car_info['bri'] = '';
            if (!empty($car_info['brithday'])) {
                $bri = strtotime($car_info['brithday']);

                $car_info['bri'] = date('m-d', $bri);
                $car_info['brithday'] = date('Y-m-d', $bri);
            }

            $this->success('获取成功', $car_info);
        } else {

            $this->error('没有找到数据');
        }
    }

}
